package com.resource.unit
{
	import com.resource.ResAsset;
	import com.resource.cache.BitmapCacher;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	/**
	 * 角色的显示对象
	 * 继承自BaseUnit，主要是对BaseUnit的frontLayer和backLayer的功能进行丰富
	 * 
	 * 注意：在资源加载完成之前设置名字等，则位置会无法调整
	 */
	public class PlayerView extends BaseUnit
	{
		protected var _nameField:TextField;
		protected var _hpBar:MovieClip;
		protected var _mpBar:MovieClip;
		protected var _isSetPos:Boolean = false; // 是否设置了名字，血蓝条等的位置
		
		public function PlayerView()
		{
			super();
			_isSetPos = false;

		}
		
		public function setName(name:String):void{
			if(!_nameField){
				_nameField = new TextField;
				_nameField.mouseEnabled = false;
				_nameField.autoSize = TextFieldAutoSize.CENTER;
				_frontLayer.addChild(_nameField);
				setPos();
			}
			_nameField.text = name;
			
		}
		
		public function setHpBar(value:Number, maxValue:Number):void{
			if(!_hpBar){
				_hpBar = new (ResAsset.hpBar);
				_frontLayer.addChild(_hpBar);
				setPos();
			}
			_hpBar.gotoAndStop(Math.ceil(_hpBar.totalFrames * value / maxValue));
		}
		
		public function setMpBar(value:Number, maxValue:Number):void{
			if(!_mpBar){
				_mpBar = new (ResAsset.mpBar);
				_frontLayer.addChild(_mpBar);
				setPos();
			}
			_mpBar.gotoAndStop(Math.ceil(_hpBar.totalFrames * value / maxValue));
		}
		
		protected function setPos():void{
			var pos:Point = getPos();
			if(pos){
				var namePos:Point = getNamePos();
				if(namePos){
					if(_nameField){
						_nameField.x = namePos.x - pos.x;
						_nameField.y = namePos.y - pos.y;
					}
				}
				var hpBarPos:Point = getHpBarPos();
				if(hpBarPos){
					if(_hpBar){
						_hpBar.x = - _hpBar.width / 2;
						_hpBar.y = - hpBarPos.y - pos.y;
					}
					if(_mpBar){
						_mpBar.x = - _mpBar.width / 2;
						_mpBar.y = - hpBarPos.y - pos.y + _mpBar.height + 1;
					}
				}
			}
		}
	}
}